using System;
using Microsoft.Xna.Framework;

namespace GameFramework.Helpers
{
    /// <summary>
    /// Useful reusable methods.
    /// </summary>
    public static class GameHelper
    {
        /// <summary>
        /// A random number generator.
        /// </summary>
        private static readonly Random random = new Random((int)DateTime.Now.Ticks);

        /// <summary>
        /// Get a random int between a specific range of values.
        /// </summary>
        /// <param name="min">The min value.</param>
        /// <param name="max">The max value.</param>
        /// <returns>The random float.</returns>
        public static int GetRandomInt(int min, int max)
        {
            return random.Next(min, max);
        }

        /// <summary>
        /// Get a random float.
        /// </summary>
        /// <returns>A random float.</returns>
        public static float GetRandomFloat()
        {
            return (float)random.NextDouble();
        }

        /// <summary>
        /// Get a random float between a specific range of values.
        /// </summary>
        /// <param name="min">The min value.</param>
        /// <param name="max">The max value.</param>
        /// <returns>The random float.</returns>
        public static float GetRandomFloat(float min, float max)
        {
            return min + (float)random.NextDouble() * (max - min);
        }

        /// <summary>
        /// Get a random vector2 inside a specific rectangle.
        /// </summary>
        /// <param name="rectangle">The rectangle.</param>
        /// <returns>A random vector2.</returns>
        public static Vector2 GetRandomVector2(Rectangle rectangle)
        {
            return GetRandomVector2(rectangle.X, rectangle.X + rectangle.Width, rectangle.Y, rectangle.Y + rectangle.Height);
        }

        /// <summary>
        /// Get a random Vector2 where each coordinate is between a specific range of values.
        /// </summary>
        /// <param name="min">The min value.</param>
        /// <param name="max">The max value.</param>
        /// <returns>The random Vector2.</returns>
        public static Vector2 GetRandomVector2(float min, float max)
        {
            return new Vector2(GetRandomFloat(min, max), GetRandomFloat(min, max));
        }

        /// <summary>
        /// Returns a random color.
        /// </summary>
        /// <returns>A random color.</returns>
        public static Color GetRandomColor()
        {
            return new Color(GetRandomFloat(), GetRandomFloat(), GetRandomFloat());
        }

        /// <summary>
        /// Get a random Vector2 where each coordinate is between a specific range of values.
        /// </summary>
        /// <param name="minx">The min x-value.</param>
        /// <param name="maxx">The max x-value.</param>
        /// <param name="miny">The min y-value.</param>
        /// <param name="maxy">The max y-value.</param>
        /// <returns>The random Vector2.</returns>
        public static Vector2 GetRandomVector2(float minx, float maxx, float miny, float maxy)
        {
            return new Vector2(GetRandomFloat(minx, maxx), GetRandomFloat(miny, maxy));
        }

        /// <summary>
        /// Catmull Rom Spline for Vector2.
        /// </summary>
        /// <param name="v1">The vector 1.</param>
        /// <param name="v2">The vector 2.</param>
        /// <param name="v3">The vector 3.</param>
        /// <param name="v4">The vector 4.</param>
        /// <param name="a">The amount.</param>
        /// <returns>The point on the curve.</returns>
        public static Vector2 CatmullRom(Vector2 v1, Vector2 v2, Vector2 v3, Vector2 v4, float a)
        {
            return new Vector2(
                MathHelper.CatmullRom(v1.X, v2.X, v3.X, v4.X, a),
                MathHelper.CatmullRom(v1.Y, v2.Y, v3.Y, v4.Y, a));
        }

        /// <summary>
        /// Returns the squared distance between two vectors.
        /// </summary>
        /// <param name="v1">The first vector.</param>
        /// <param name="v2">The second vector.</param>
        /// <returns>The squared distance.</returns>
        public static float SquaredDistance(Vector2 v1, Vector2 v2)
        {
            return (v1.X - v2.X) * (v1.X - v2.X) + (v1.Y - v2.Y) * (v1.Y - v2.Y);
        }
    }
}
